﻿@model IEntityViewModel
@{
    object area = ViewContext.RouteData.DataTokens["Area"];
    if (area == null)
    {
        Layout = "~/Views/Shared/_DialogLayout.cshtml";
    }
    else
    {
        Layout = "~/Areas/" + area.ToString() + "/Views/Shared/_DialogLayout.cshtml";
    }
    ViewBag.Title = Model.Metadata.Name + " Multiple Selector";
}
<div class="row">
    @if (Model.Parent != null)
    {
        <div class="col-md-3" id="selectorparentview">
            <ul>
                <li class="jstree-open">
                    <a href="@Html.Raw(Url.ClearQueryPath("page"))">全部</a>
                    <ul>
                        @Html.Partial("ParentView", Model.Parent)
                    </ul>
                </li>
            </ul>
        </div>
    }
    <div class="@(Model.Parent == null ? "col-md-12":"col-md-9")">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th class="text-center" width="36"></th>
                    @foreach (var header in Model.Headers)
                    {
                        <th title="@header.Description">@(header.ShortName ?? header.Name)</th>
                    }
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.Items)
                {
                    <tr>
                        <td class="text-center">
                            <input type="checkbox" data-name="@item" value="@item.Index" />
                        </td>
                        @foreach (var header in Model.Headers)
                        {
                            <td>@EntityValueConverter.GetConverter(header).ConvertTo(header.GetValue(item), typeof(string))</td>
                        }
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        @Html.PaginationButton(Model)
    </div>
    <div class="col-md-4 text-center">
        <div class="btn-group">
            @if (Model.Metadata.SearchProperties.Count() > 0)
            {
                <a class="btn btn-default" href="@Html.Raw(Url.Action("Search",new {actionName = "MultipleSelector"}))">查找</a>
            }
            <button class="btn btn-default" onclick="ok()">确定</button>
        </div>
    </div>
    <div class="col-md-4 text-right">
        @Html.Pagination(Model)
    </div>
</div>
<script type="text/javascript">
    function ok() {
        var items = $(':checked[value]');
        if (items.length == 0) {
            alert('Please select at least one.');
            return;
        }
        var result = new Array();
        items.each(function (index, element) {
            element = $(element);
            var id = element.val();
            var name = element.attr('data-name');
            result.push('<option value=' + id + '>' + name + '</option>');
        });
        window.returnValue = result;
        if (window.opener) {
            window.opener.returnValue = window.returnValue;
            if (window.opener.selectCompleted != undefined)
                window.opener.selectCompleted(window.returnValue);
        }
        window.close();
    }
</script>